package hu.diploma.android.serviceimpl;

import hu.diploma.android.util.database.PeopleDataUtil;
import hu.diploma.common.enums.DataType;
import hu.diploma.common.service.PersonalDataService;
import hu.diploma.common.vos.PersonVo;
import hu.diploma.common.vos.PersonalDataVo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;

public class PersonalDataServiceImpl implements PersonalDataService {
	private final ContentResolver contentResolver;
	private final PeopleDataUtil dataUtil;

	public PersonalDataServiceImpl(ContentResolver contentResolver) {
		super();
		this.contentResolver = contentResolver;
		dataUtil = new PeopleDataUtil(contentResolver);
	}

	@Override
	public PersonalDataVo save(PersonalDataVo peopleData) {

		if (peopleData.getType().equals(DataType.EMAIL)) {
			return dataUtil.mergeEmail(peopleData);
		}
		if (peopleData.getType().equals(DataType.EVENT)) {
			return dataUtil.mergeEvent(peopleData);
		}
		if (peopleData.getType().equals(DataType.IM)) {
			return dataUtil.mergeIm(peopleData);
		}
		if (peopleData.getType().equals(DataType.NICKNAME)) {
			return dataUtil.mergeNickname(peopleData);
		}
		if (peopleData.getType().equals(DataType.NOTE)) {
			return dataUtil.mergeNote(peopleData);
		}
		if (peopleData.getType().equals(DataType.ORGANIZATION)) {
			return dataUtil.mergeOrganization(peopleData);
		}
		if (peopleData.getType().equals(DataType.PHONE)) {
			return dataUtil.mergePhone(peopleData);
		}
		if (peopleData.getType().equals(DataType.WEBSITE)) {
			return dataUtil.mergeWebsite(peopleData);
		}
		return null;
	}

	@Override
	public PersonalDataVo merge(PersonalDataVo peopleData) {
		if (peopleData.getType().equals(DataType.EMAIL)) {
			return dataUtil.mergeEmail(peopleData);
		}
		if (peopleData.getType().equals(DataType.EVENT)) {
			return dataUtil.mergeEvent(peopleData);
		}
		if (peopleData.getType().equals(DataType.IM)) {
			return dataUtil.mergeIm(peopleData);
		}
		if (peopleData.getType().equals(DataType.NICKNAME)) {
			return dataUtil.mergeNickname(peopleData);
		}
		if (peopleData.getType().equals(DataType.NOTE)) {
			return dataUtil.mergeNote(peopleData);
		}
		if (peopleData.getType().equals(DataType.ORGANIZATION)) {
			return dataUtil.mergeOrganization(peopleData);
		}
		if (peopleData.getType().equals(DataType.PHONE)) {
			return dataUtil.mergePhone(peopleData);
		}
		if (peopleData.getType().equals(DataType.WEBSITE)) {
			return dataUtil.mergeWebsite(peopleData);
		}
		return null;
	}

	@Override
	public PersonalDataVo findById(Serializable searchId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<PersonalDataVo> loadAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void delete(PersonalDataVo vo) {
		// TODO Auto-generated method stub

	}

	@Override
	public ArrayList<PersonalDataVo> findByPeople(PersonVo peopleVo) {
		ArrayList<PersonalDataVo> peopleDataVos = new ArrayList<PersonalDataVo>();
		try {

			peopleDataVos.addAll(dataUtil.loadEmailByPeople(peopleVo.getPhoneId()));
			peopleDataVos.addAll(dataUtil.loadEventByPeople(peopleVo.getPhoneId()));
			peopleDataVos.addAll(dataUtil.loadIMByPeople(peopleVo.getPhoneId()));
			peopleDataVos.addAll(dataUtil.loadPhoneByPeople(peopleVo.getPhoneId()));
			peopleDataVos.addAll(dataUtil.loadWebsiteByPeople(peopleVo.getPhoneId()));
			peopleDataVos.add(dataUtil.loadNicknameByPeople(peopleVo.getPhoneId()));
			peopleDataVos.add(dataUtil.loadNotesByPeople(peopleVo.getPhoneId()));
			peopleDataVos.add(dataUtil.loadOrganizationByPeople(peopleVo.getPhoneId()));

		} catch (Exception e) {
			// TODO: handle exception
		}
		return peopleDataVos;

	}

	@Override
	public PersonalDataVo findByPhoneId(Serializable searchId) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
